//
//  NearByViewController.m
//  CloudWeibo
//
//  Created by CLOUD on 2/1/15.
//  Copyright (c) 2015 Cloud. All rights reserved.
//

#import "NearByViewController.h"
#import "UIImageView+WebCache.h"


@interface NearByViewController ()

@end

@implementation NearByViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.isCancelButton = YES;
        self.isBackButton = NO;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"我在这里";
    self.tableView.hidden = YES;
    [super showLoading:YES];
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    [locationManager startUpdatingLocation];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}

- (void)locationManager:(CLLocationManager *)manager
	didUpdateToLocation:(CLLocation *)newLocation
		   fromLocation:(CLLocation *)oldLocation
{
    [manager stopUpdatingLocation];
    if (self.data == nil) {
        float longitude = newLocation.coordinate.longitude;
        float latitude = newLocation.coordinate.latitude;
        
        NSString *longStirng = [NSString stringWithFormat:@"%f",longitude];
        NSString *latString = [NSString stringWithFormat:@"%f",latitude];
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:longStirng,@"long",latString,@"lat", nil];
        [self.sinaweibo requestWithURL:@"place/nearby/pois.json" params:params httpMethod:@"GET" block:^(id result){
            [self loadNearByDataFinish:result];
        }];
    }
}

- (void)refreshUI
{
    self.tableView.hidden = NO;
    [super showLoading:NO];
    [self.tableView reloadData];
}
         
- (void)loadNearByDataFinish:(NSDictionary *)result{
    NSArray *pois = [result objectForKey:@"pois"];
    self.data = pois;
    [self refreshUI];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identify] autorelease];
    }
    NSDictionary *dic = [self.data objectAtIndex:indexPath.row];
    NSString *title = [dic objectForKey:@"title"];
    NSString *address = [dic objectForKey:@"address"];
    NSString *icon = [dic objectForKey:@"icon"];
    
    cell.textLabel.text = title;
    if (![address isEqual:[NSNull null]]) {
        cell.detailTextLabel.text = address;
    }

    [cell.imageView setImageWithURL:[NSURL URLWithString:icon]];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectBlock != nil) {
        NSDictionary *dic = [self.data objectAtIndex:indexPath.row];
        _selectBlock(dic);
        Block_release(_selectBlock);
        _selectBlock = nil;
    }
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_tableView release];
    [super dealloc];
}
@end
